﻿<%@ WebHandler Language="C#" Class="login" %>

using System;
using System.Web;
using OneshoppingWeb;
using OneShoppingModel;
using OneShoppingBLL;
using OneShoppingUtility;
using System.Web.SessionState;

public class login : IHttpHandler, IRequiresSessionState
{
    
    public void ProcessRequest (HttpContext context) 
    {
        string LoginName = context.Request["LoginName"];
        string LoginPwd = context.Request["LoginPwd"];
        string referrer = "";
        SystemUser sysUser = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == LoginName && r.LoginPwd == LoginPwd);
        try
        {
            referrer = Convert.ToString(context.Request.UrlReferrer);//获取那个页面访问该接口
            if (referrer == "") 
            {
                context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ILLEGAL_ACCESS));//非法访问
                return;
            }
        }
        catch {
            context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ILLEGAL_ACCESS));//非法访问
            return;
        }
       
        if (sysUser != null)
        {
            if (referrer.IndexOf("Front") != -1)
            {
                if(sysUser.RoleId!=3)
                {
                    context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.LOGIN_FAIL));//登入失败
                    return;
                }
               // str = referrer.Substring(referrer.IndexOf("Front"), referrer.Length - referrer.IndexOf("Front"));

                if (referrer.IndexOf("Front/index.html") == -1)
                {
                    context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ILLEGAL_ACCESS));//非法访问
                    return;
                }
            }

            if (referrer.IndexOf("Backstage") != -1)
            {
                if (sysUser.RoleId == 3)
                {
                    context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.LOGIN_FAIL));//登入失败
                    return;
                }
            //    str = referrer.Substring(referrer.IndexOf("Backstage"), referrer.Length - referrer.IndexOf("Backstage"));
                if (referrer.IndexOf("Backstage/login.html") == -1)
                {
                    context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ILLEGAL_ACCESS));//非法访问
                    return;
                }
            }
            
            
            Role role = RoleBLL.GetInstance().GetEntity(r => r.RoleId == sysUser.RoleId);
            if (role.IsUsing==0)
            {
                context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ROLE_N_USE));//角色没有使用
                return;
            }
            if (sysUser.Status==0) {
                context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.USER_N_USE));//用户不能使用
                return;
            }        
            context.Session.Add("user", sysUser);//登入成功将用户存入session
            context.Session.Add("RoleId", sysUser.RoleId);//登入成功将RoleId存入session
            context.Response.Write(UtilityJson.ResEntityToJson((int)Protocol_user.LOGIN_SUCCESS,sysUser));
        }
        else 
        {
            context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.LOGIN_FAIL));
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}